//
//  RepaymentViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/9.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import SwiftyJSON

class RepaymentViewController: KMViewController {
    
    fileprivate let cellId = "repaymentCell"
    fileprivate var dataArray = [RepaymentModel]()
    fileprivate var tableView : UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = repaymentReminder
        createTableView()
        addRightButton()
        getData()

    }
    
    //MARK: 创建tableView
    fileprivate func createTableView() {
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(RepaymentCell.self, forCellReuseIdentifier: cellId)
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }
    
    //MARK: 添加右边按钮
    func addRightButton() {
        
        let barButton = UIBarButtonItem.init(image: #imageLiteral(resourceName: "cardadd"), style: .plain, target: self, action: #selector(rightAction))
        self.navigationItem.rightBarButtonItem = barButton
        
    }
    
    //MARK: 右边点击事件
    @objc fileprivate func rightAction() {
        
        let addRepayVC = AddRepaymentViewController()
        addRepayVC.addBlock = {
            
            self.getData()
        }
        self.navigationController?.pushViewController(addRepayVC, animated: true)
        
    }

    //MARK: 获取数据
    fileprivate func getData() {
        
        KMTool.waiting()
        KMTool.requestMethod(.post, URLString: url_repaymentList, parameters: ["user_name":KMTool.stringForEnumKey(.phone) ?? ""], success: { (value, json) in
            
            KMTool.stop()
            if json["status"].stringValue == "0" {
                
                self.dataArray.removeAll()
                
                let dataString = json["data"].stringValue.data(using: String.Encoding.utf8)
                
                let data = JSON.init(dataString!)
                
                if let jsonData = data.arrayObject {
                    
                    for item in jsonData {
                        let model = RepaymentModel.setModel(with: item as! [String : Any])
                        self.dataArray.append(model)
                    }
                    
                    self.tableView.reloadData()
                }
                
            }
            
        }) {
            
            KMTool.stop()
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    


}

extension RepaymentViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 160)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? RepaymentCell
        cell?.selectionStyle = .none
        cell?.model = dataArray[indexPath.row]
        return cell!
    }
    
    //MARK: 设置滑动删除
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        
        return true
    }
    
    //MARK: 删除
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        
        KMTool.requestMethod(.post, URLString: url_deleteRepayment, parameters: ["id":dataArray[indexPath.row].id ?? ""], success: { (value, json) in
            
            if json["status"].stringValue == "0" {
                self.getData()
            }
            
            KMTool.showText(_text: json["data"].stringValue)
            
        }) {
            
        }
    }
    
    //MARK: 修改按钮文字
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        
        return delete_title
    }
    
}
